﻿
Public Class PageHeader
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try

            'If user is logged in, hide loging/register and display welcome message
            If Not Session("User_id") Is Nothing Then

                liLogin.Visible = False
                liRegister.Visible = False
                liLogout.Visible = True
                liUserName.Visible = True
                liDashboard.Visible = True
                liUserSettings.Visible = True

                If Not HttpContext.Current.Session("Email") Is Nothing Then

                    lblUserName.Text = HttpContext.Current.Session("Email")

                End If

            Else

                liLogin.Visible = True
                liRegister.Visible = True
                liLogout.Visible = False
                liUserName.Visible = False
                liDashboard.Visible = False
                liUserSettings.Visible = False

            End If

        Catch ex As Exception



        End Try

    End Sub

    Private Sub cmdLogout_Click(sender As Object, e As System.EventArgs) Handles cmdLogout.Click

        Try

            HttpContext.Current.Session("User_ID") = Nothing

            Response.Redirect("~/Default.aspx", True)

        Catch ex As Exception

        End Try

    End Sub

    Private Sub cmdLogin_Click(sender As Object, e As System.EventArgs) Handles cmdLogin.Click

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcUsers As MongoCollection(Of Trebuchet.User) = Nothing
        Dim mcUserCursor As MongoCursor = Nothing
        Dim uUser As Trebuchet.User = Nothing

        Try

            If modShared.UserLogin(txtEmail.Text, txtPassword.Text, Nothing) Then

                mpeLogin.Hide()

                Response.Redirect("Dashboard.aspx")

            End If

        Catch ex As Exception

            LoginMessageDisplay.AddMessage(ex.Message)

        Finally

            mdDatabase = Nothing
            mcUsers = Nothing
            mcUserCursor = Nothing
            uUser = Nothing

        End Try

    End Sub

    Private Sub cmdRegister_Click(sender As Object, e As System.EventArgs) Handles cmdRegister.Click

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcUsers As MongoCollection(Of Trebuchet.User) = Nothing
        Dim mcUserCursor As MongoCursor = Nothing
        Dim uUser As Trebuchet.User = Nothing
        Dim bolSave As Boolean = True

        Try

            'Validate Controls
            If txtRegisterEmail.Text.Trim = "" Then

                bolSave = False

                RegisterMessageDisplay.AddMessage("Email is required")

            End If

            If txtRegisterPassword.Text.Trim = "" Then

                bolSave = False

                RegisterMessageDisplay.AddMessage("Password is required.")

            Else

                If txtRegisterPassword.Text.Trim <> txtRegisterConfirmPassword.Text.Trim Then

                    bolSave = False

                    RegisterMessageDisplay.AddMessage("Passwords must match.")

                End If

            End If

            'Check to see if the user or email already exists in the system
            mdDatabase = DBHelper.GetDataBase()
            mcUsers = mdDatabase.GetCollection(Of Trebuchet.User)("Users")

            mcUserCursor = mcUsers.FindAs(Of Trebuchet.User)(Query.And(Query.EQ("Email", txtRegisterEmail.Text.Trim), Query.EQ("Active", True)))

            If Not mcUserCursor Is Nothing _
                AndAlso mcUserCursor.Count > 0 Then

                bolSave = False

                'Username already exists, display message
                RegisterMessageDisplay.AddMessage("Email already in use.")

            End If

            If bolSave Then

                'Save new user
                uUser = New Trebuchet.User

                uUser.Email = txtRegisterEmail.Text.Trim
                uUser.Password = Encryption.AES_Encrypt(txtPassword.Text.Trim, Encryption.strSharedAESPassword)

                mcUsers.Save(uUser)

                'Log user in
                Session("User_id") = uUser._id
                Session("Email") = uUser.Email

                mpeRegister.Hide()

                Response.Redirect("Dashboard.aspx")

            End If

        Catch ex As Exception

            RegisterMessageDisplay.AddMessage(ex.Message)

        Finally

            mdDatabase = Nothing
            mcUsers = Nothing
            mcUserCursor = Nothing
            uUser = Nothing

        End Try

    End Sub

End Class